package utils.jn;

/**
 * A room sit in the theater
 * 
 * @author Joao Nuno
 * 
 */
public class TheaterSit implements Sit {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7550087272134526478L;

	/**
	 * the row number
	 */
	private final int row;

	/**
	 * The column number
	 */
	private final int column;

	/**
	 * The state of this sit
	 */
	private State state;

	/**
	 * The id of the purchase or 0 if is a free sit
	 */
	private int purchaseID;

	/**
	 * Constructor
	 * 
	 * @param row
	 *            Row number
	 * @param column
	 *            Column number
	 */
	public TheaterSit(int row, int column) {
		this.row = row;
		this.column = column;
		this.state = State.FREE;
		this.purchaseID = 0;
	}

	/**
	 * Free a reservation for this sit
	 * 
	 * @param purchaseID
	 *            The purchaser id
	 * @throws Exception
	 */
	public void occupy(int purchaseID) {
		this.state = State.OCCUPIED;
		this.purchaseID = purchaseID;
	}

	/**
	 * Free a reservation for this sit
	 * 
	 * @throws Exception
	 */
	public void free() {
		this.state = State.FREE;
		this.purchaseID = 0;
	}

	public Sit reserve() {
		TheaterSit other = new TheaterSit(this.row, this.column);
		other.state = State.RESERVED;
		return other;
	}

	/**
	 * toString method. Show the row and the column of this sit separated by
	 * commas.
	 */
	public String toString() {
		return this.row + "," + this.column + " - " + this.state
				+ (this.state.isOccupied() ? " by " + this.purchaseID : "");
	}

	@Override
	public int getRow() {
		return this.row;
	}

	@Override
	public int getColumn() {
		return this.column;
	}

	@Override
	public State state() {
		return this.state;
	}
	
	@Override
	public int hashCode(){
		int hash = 7;
		hash = 31 * hash + row;
		hash = 31 * hash + column;
		hash = 31 * hash + (null == state ? 0 : state.hashCode());
		return hash;
	}

	@Override
	public boolean equals(Object other) {
		if (other instanceof Sit)
			return this.getRow() == ((Sit) other).getRow()
					&& this.getColumn() == ((Sit) other).getColumn();
		return false;
	}
}
